package logics
{
	import global.BillState;
	import global.Managers;
	import global.WorldType;
	
	import net.servers.ServerManager;
	
	import ui.faces.FaceManager;
	import ui.panels.OrderedDishPanel;
	import ui.panels.PanelType;
	
	import utils.pool.ObjectPool;
	
	import vo.BillDishVO;
	import vo.BillVO;
	import vo.DishStatus;
	import vo.DishVO;
	
	import worlds.BaseWorld;
	
	public class BillManager
	{
		public var billVO					: BillVO;
		
		public function BillManager()
		{
		}
		public function init(serverManager:ServerManager, tableManager:TableManager):void {
			billVO = new BillVO();
			billVO.id = 0;
			billVO.tableID = tableManager.id;
			billVO.tempDishes = [];
			billVO.dishes = [];
			billVO.totalPrice = 0;
			billVO.state = BillState.PAYED;
			billVO.beginTime = 0;
			billVO.endTime = 0;
			
			serverManager.subscribe("GetBill", getBillHandler);
			serverManager.subscribe("DishStatusChanged", dishStatusChangedHandler);
			serverManager.subscribe("CreateBill", createBillHandler);
			serverManager.subscribe("ApplyPay", applyPayBillHandler);
			serverManager.subscribe("TableApplyPay", tableApplyPayBillHandler);
			serverManager.subscribe("PayBill", payBillHandler);
			serverManager.subscribe("CancelBill", cancelBillHandler);
			serverManager.subscribe("AddDishes", addDishesHandler);
			serverManager.subscribe("RemoveDishes", removeDishesHandler);
			
			getBill();
		}
		private function getBillHandler(data:Object):void {
			var tableID:int = data.tableID;
			if(tableID != billVO.tableID) return;
			billVO.id = data.id;
			billVO.state = data.status;
			billVO.totalPrice = data.totalPrice;
			billVO.beginTime = data.beginTime;
			var dishArr:Array = billVO.dishes;
			ObjectPool.clear(dishArr);
			var dishes:Array = data.Dishes;
			for(var i:int = 0; i < dishes.length; i++) {
				var dish:Object = dishes[i];
				var value:BillDishVO = ObjectPool.pop(BillDishVO) as BillDishVO;
				value.dishID = int(dish.id);
				value.status = int(dish.status);
				dishArr.push(value);
			}
			// 刷新已选菜单面板
			var panel:OrderedDishPanel = Managers.panelManager.getPanel(PanelType.ORDERED_DISH) as OrderedDishPanel;
			panel.update();
			// 如果在闲置界面，则跳转到工作界面
			var manager:FaceManager = Managers.faceManager;
			if(manager.currentFace == manager.idleFace) {
				manager.switchTo(manager.mainFace);
			}
		}
		private function dishStatusChangedHandler(data:Object):void {
			var dishID:int = data.dishId;
			var status:int = data.status;
			var dishArr:Array = billVO.dishes;
			for each(var value:BillDishVO in dishArr) {
				if(value.dishID == dishID) {
					value.status = status;
					break;
				}
			}
			// 刷新已选菜单面板
			var panel:OrderedDishPanel = Managers.panelManager.getPanel(PanelType.ORDERED_DISH) as OrderedDishPanel;
			panel.update();
			// 如果在闲置界面，则跳转到工作界面
			var manager:FaceManager = Managers.faceManager;
			if(manager.currentFace == manager.idleFace) {
				manager.switchTo(manager.mainFace);
			}
		}
		private function createBillHandler(data:Object):void {
			billVO.id = data.billID;
			var manager:FaceManager = Managers.faceManager;
			if(manager.currentFace == manager.idleFace) {
				manager.switchTo(manager.mainFace);
			}
		}
		private function applyPayBillHandler(data:Object):void {
			// 服务器接收到申请
			if(BaseWorld.worldType != WorldType.TABLE_WORLD) return;
		}
		private function tableApplyPayBillHandler(data:Object):void {
			// 有客户端申请结账
			if(BaseWorld.worldType != WorldType.MANAGEMENT_WORLD) return;
			var dishManager:DishManager = Managers.dishManager;
			var dishes:Array = data.dishes;
			var cost:Number = 0;
			for(var i:int = 0; i < dishes.length; i++) {
				var dishID:int = dishes[i];
				var dishVO:DishVO = dishManager.getValueByID(dishID);
				cost += dishVO.price;
			}
		}
		private function payBillHandler(data:Object):void {
			billVO.id = 0;
			billVO.tableID = Managers.tableManager.id;
			billVO.dishes.splice(0, billVO.dishes.length);
			billVO.totalPrice = 0;
			billVO.state = BillState.PAYED;
			billVO.beginTime = 0;
			billVO.endTime = 0;
		}
		private function cancelBillHandler(data:Object):void {
			billVO.id = 0;
			billVO.tableID = Managers.tableManager.id;
			billVO.dishes.splice(0, billVO.dishes.length);
			billVO.totalPrice = 0;
			billVO.state = BillState.CANCELED;
			billVO.beginTime = 0;
			billVO.endTime = 0;
		}
		private function addDishesHandler(data:Object):void {
			// 添加菜品成功，将临时菜品移动到确定菜品列表中
			var temp:Array = billVO.tempDishes;
			var dishes:Array = billVO.dishes;
			while(temp.length > 0) {
				dishes.push(temp.pop());
			}
			// 最后请求刷新账单
			getBill();
		}
		private function removeDishesHandler(data:Object):void {
		}
		
		public function addDish(dishID:int):void {
			var value:BillDishVO = ObjectPool.pop(BillDishVO) as BillDishVO;
			value.dishID = dishID;
			value.status = DishStatus.WAITING_FOR_MAKING;
			billVO.tempDishes.push(value);
		}
		public function removeDish(dishID:int):void {
			
		}
		public function sendBill():void {
			var data:Object = {id:billVO.tableID, dishes:[]};
			for each(var value:BillDishVO in billVO.tempDishes) {
				data.dishes.push(value.dishID);
			}
			Managers.serverManager.send("AddDishes", data);
		}
		public function getBill():void {
			Managers.serverManager.send(new XML("<GetBill tableID='" + billVO.tableID + "'/>"));
		}
		public function createBill():void {
			Managers.serverManager.send("CreateBill");
		}
		public function applyPayBill():void {
			Managers.serverManager.send("ApplyPay");
		}
		public function payBill():void {
			Managers.serverManager.send(new XML("<PayBill id='" + billVO.tableID + "'/>"));
		}
		public function cancelBill():void {
			
		}
	}
}